﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MoveSquare : MonoBehaviour {

    private bool SpacePressed;
	private bool CutEnable;
    private List<Vector2> toCut;
    public PaperSheet scriptToCall;
    private int count;

	// Use this for initialization
	void Start () {
        toCut = new List<Vector2>();
        SpacePressed = false;
		CutEnable = false;
        count = 0;
	}
	
	// Update is called once per frame
	void Update () {
        float x = Input.GetAxis("Horizontal") * Time.deltaTime * (-1);
        float y = Input.GetAxis("Vertical") * Time.deltaTime;

        if(transform.position.x + x > (float)0.75)
        {
            x = (float)0.75 - transform.position.x;
        }

        if (transform.position.x + x < -(float)0.75)
        {
            x = -(float)0.75 - transform.position.x;
        }

        if (transform.position.y + y > (float)0.75)
        {
            y = (float)0.75 - transform.position.y;
        }

        if (transform.position.y + y < -(float)0.75)
        {
            y = -(float)0.75 - transform.position.y;
        }

        if (Input.GetKeyDown(KeyCode.Space) && !SpacePressed)
        {
            SpacePressed = true;
			
			if (!CutEnable)
			{
				CutEnable = true;
				
				float x1 = transform.position.x - (float)0.25;
				float y1 = transform.position.y - (float)0.25;
				
				float x2 = transform.position.x + (float)0.25;
				float y2 = transform.position.y + (float)0.25;
				
				toCut.Add(new Vector2(x1, y1));
				toCut.Add(new Vector2(x2, y1));
				toCut.Add(new Vector2(x2, y2));
				toCut.Add(new Vector2(x1, y2));
				
				scriptToCall.Cut(toCut);
				
				transform.renderer.enabled = false;
			}
			
			else
			{
				CutEnable = false;
				scriptToCall.Cut(null);
				
				transform.renderer.enabled = true;
			}
		}
		
        else if(Input.GetKeyUp(KeyCode.Space))
        {
            SpacePressed = false;
        }

		if (!CutEnable)
			transform.Translate(new Vector3(x, y, 0));
	}
}
